package system;

import java.io.File;
import java.util.Hashtable;

public class SoundLibrary {
	Sound[] sounds;
	int numSounds;
	Hashtable<String, Integer> location;
	boolean muted = false;

	public SoundLibrary() {

		location = new Hashtable<String, Integer>();		
		File dir = new File("assets/audio/");
		String[] children = dir.list();
		sounds = new Sound[children.length];

		// for each file in the directory, create a sound object for it and add to hash table		
		if (children != null) {
			for (int i=0; i<children.length; i++) {
				location.put(children[i], i);
				if(children[i].startsWith("bgm")){
					sounds[i] = new Sound(children[i], true);
				} else {
					sounds[i] = new Sound(children[i], false);
				}
			}
		}

	}

	// takes as input a filename and plays that sound file 
	// note: input filename should include extension
	public void Play(String filename){
		if (!muted) {
			if (location.get(filename) != null){
				sounds[location.get(filename)].Play();
			} else {
				System.out.println("could not play sound " + filename);
			}
		}
	}

	public void Stop(String filename){
		if (location.get(filename) != null){
			sounds[location.get(filename)].Stop();
		} else {
			System.out.println("could not stop sound " + filename);
		}
	}

	public void StopAll() {
		for(int i=0; i<sounds.length; i++){
			sounds[i].Stop();
		}

	}

	// sets the mute flag
	public void ChangeMuteState(boolean newState, Scene currentScene){
		muted = newState;
		if(newState){
			StopAll();
		} else {
			switch (currentScene.game.getState()){
			case Game.GAME_OVER:
				Play("bgm-gameover.wav");
				break;
			case Game.GAME_EXIT:
				break;
			case Game.GAME_HIGHSCORES:
				Play("bgm-topscores.wav");
				break;
			case Game.GAME_RUN:
				Play("bgm-play.wav");
				break;
			case Game.GAME_START:
				if(currentScene.isDone() ){
					Play("bgm-title.wav");	
				}
						
			}
		}
	}

	// sets the muted state
	public boolean IsMuted(){
		return muted;
	}

}
